import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
//配置src为@
import path from 'path'
//svg配置
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import {viteMockServe} from "vite-plugin-mock";
// https://vitejs.dev/config/
export default defineConfig(({command,mode})=> {
  //获取各个环境下的对应的变量
  const env = loadEnv(mode,process.cwd());
  return {
    plugins: [
      vue(),
      //svg
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        // Specify symbolId format
        symbolId: 'icon-[dir]-[name]',
      }),
      viteMockServe({
        localEnabled:command==='serve'
      })
    ],
    // src路径配置
    resolve: {
      alias: {
        '@': path.resolve('./src'),
      },
    },
    //scss全局变量配置
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      }
    },
    //代理跨域
    server:{
      proxy:{
        [env.VITE_APP_BASE_API]:{
          //获取数据的服务器地址设置
          target:env.VITE_SERVE,
          //需要代理跨域
          changeOrigin:true,
          //路径重写
          rewrite:(path)=>path.replace(/^\/api/,''),
        }
      }
      // 'api':{
      //   target:''
      // }
    }
  }
})
